/*
 * File: cookieServlet.java Author: Robert Bittle <guywithnose@gmail.com>
 */
package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import foxtrotbravo.Config;

/**
 * @author Dude
 * 
 */
public class FoxtrotBravoServlet extends HttpServlet
{

  /**
   * Get Cookie.
   * 
   * @param req
   *          the req
   * @param cookieName
   *          the cookie name
   * @return the cookie
   */
  protected static String getCookie(HttpServletRequest req, String cookieName)
  {
    Cookie[] cookies = req.getCookies();
    if (cookies != null)
      for (int i = 0; i < cookies.length; i++)
      {
        if (cookies[i].getName().equals(cookieName))
          return cookies[i].getValue();
      }
    return null;
  }

  protected static boolean isDevMode(HttpServletRequest req)
  {
    // Cookie is "false" on live
    return ("false".equals(getCookie(req, "useMinified")) && !Config.isDev())
    // Or cookie is not "true" on dev
        || (!"true".equals(getCookie(req, "useMinified")) && Config.isDev());
  }

  protected static String errorJson(String error)
  {
    JSONObject JO = new JSONObject();
    try
    {
      JO.put("success", false);
      JO.put("error", error);
    } catch (JSONException e)
    {
      e.printStackTrace();
    }
    return JO.toString();
  }

  protected static String successJson()
  {
    JSONObject JO = new JSONObject();
    try
    {
      JO.put("success", true);
    } catch (JSONException e)
    {
      e.printStackTrace();
    }
    return JO.toString();
  }
  
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException
  {
      doGet(req, resp);
  }

}
